import java.util.PriorityQueue;

public class KthLargest03 {
	private PriorityQueue<Integer> minHeap;
	private int size;

	public KthLargest03(int k, int[] nums) {
		size = k;
		minHeap = new PriorityQueue<>();
		for (int i = 0; i < nums.length; i++) {
			// 调用本类的add，在初始化时就保证堆的大小最多为k
			add(nums[i]);
		}
	}

	public int add(int val) {
		minHeap.offer(val);
		if (minHeap.size() > size) {
			minHeap.poll();
		}
		return minHeap.peek();
	}
}
